﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
    public partial class RespondTickets : System.Web.UI.Page
    {

        private Collection<Ticket> _newestTickets;
        private Collection<Ticket> _priorityTickets;
        private const int ticketsByPage = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            else
            {
                if (WebmasterManager.IsWebmaster(User.Identity.Name) && WebmasterDataAccess.GetWebmasterByUserId(UserDataAccess.GetUserByEmail(User.Identity.Name).Id).
                        CanRespondTickets)
                {
                    FillDataLists();
                    MultiViewCanReply.ActiveViewIndex = 0;
                }
                else
                {
                    if (WebmasterManager.IsWebmaster(User.Identity.Name))
                        MultiViewCanReply.ActiveViewIndex = 1;
                    else
                    {
                        Response.Write(
                            "<script>if(window.history.length>1){history.back();} else {window.location.href = 'Default.aspx';}</script>");
                    }
                }
            } 
        }

        private void FillDataLists()
        {
            _newestTickets = TicketManager.GatherNewestTickets();
            _priorityTickets = TicketManager.GatherHigherPriorityTickets();
            if (_newestTickets == null)
                MultiViewNewest.ActiveViewIndex = 1;
            else
            {
                if (_newestTickets.Count < 0)
                    MultiViewNewest.ActiveViewIndex = 1;
                else
                {
                    if (!IsPostBack)
                    {
                        Session["newestNumPage"] = 1;
                        ChangeNewestTicketsPage();
                    }
                    MultiViewNewest.ActiveViewIndex = 0;
                }
            }
            if (_priorityTickets == null)
                MultiViewPriority.ActiveViewIndex = 1;
            else
            {
                if (_priorityTickets.Count < 0)
                    MultiViewPriority.ActiveViewIndex = 1;
                else
                {
                    if (!IsPostBack)
                    {
                        Session["priorityNumPage"] = 1;
                        ChangePriorityTicketsPage();
                    }
                    MultiViewPriority.ActiveViewIndex = 0;
                }
            }
        }

        private bool bSwitch = false;
        string color1 = "#ffffcc";
        string color2 = "#ccff99";

        public string TRJavaScript(Control con)
        {
            string tmp;
            DataListItem dli = con as DataListItem;
            Button btn = dli.FindControl("HiddenButton") as Button;
            string _js = "bgcolor={0} onMouseover='rowcolor=this" +
                         ".style.backgroundColor;this.style.backgroundColor" +
                         "=\"#3670C7\"; this.style.color=\"white\"; this.style.cursor = \"hand\"' " +
                         "onMouseout='this.style.backgroundColor=\"white\"; this.style.color=\"black\";' " +
                         " onclick='document.getElementById(\"{1}\").click();' ";
            tmp = bSwitch ? string.Format(_js, color1, btn.ClientID) :
                           string.Format(_js, color2, btn.ClientID);
            bSwitch = !bSwitch;
            return tmp;
        }

        private void ChangeNewestTicketsPage()
        {
            PrevButton.Enabled = true;
            NextButton.Enabled = true;
            int max = _newestTickets.Count;
            int numPage = int.Parse(Session["newestNumPage"].ToString());
            if (numPage == 1)
                PrevButton.Enabled = false;
            if ((max - ((numPage - 1) * ticketsByPage)) < ticketsByPage)
                NextButton.Enabled = false;
            ShowContent.InnerText = "Showing " + (((numPage - 1) * ticketsByPage) + 1) + "-" + ((((numPage) * ticketsByPage) > max) ? max : ((numPage) * ticketsByPage)) + " of " + max;
            if (_newestTickets.Count == 0)
            {
                MultiViewNewest.ActiveViewIndex = 1;
            }
            else
            {
                Collection<Ticket> actual = new Collection<Ticket>();
                int min = (numPage - 1) * ticketsByPage;
                int maxElem = (numPage * ticketsByPage) - 1;
                for (int i = 0; i <= maxElem && i < _newestTickets.Count; i++)
                {
                    if (i >= min && i <= maxElem)
                    {
                        actual.Add(_newestTickets[i]);
                    }
                }
                dataListNewestTickets.DataSource = actual;
                dataListNewestTickets.DataBind();
                MultiViewNewest.ActiveViewIndex = 0;
            }
        }

        private void ChangePriorityTicketsPage()
        {
            PrevButton2.Enabled = true;
            NextButton2.Enabled = true;
            int max = _priorityTickets.Count;
            int numPage = int.Parse(Session["priorityNumPage"].ToString());
            if (numPage == 1)
                PrevButton2.Enabled = false;
            if ((max - ((numPage - 1) * ticketsByPage)) < ticketsByPage)
                NextButton2.Enabled = false;
            ShowContent2.InnerText = "Showing " + (((numPage - 1) * ticketsByPage) + 1) + "-" + ((((numPage) * ticketsByPage) > max) ? max : ((numPage) * ticketsByPage)) + " of " + max;
            if (_priorityTickets.Count == 0)
            {
                MultiViewPriority.ActiveViewIndex = 1;
            }
            else
            {
                Collection<Ticket> actual = new Collection<Ticket>();
                int min = (numPage - 1) * ticketsByPage;
                int maxElem = (numPage * ticketsByPage) - 1;
                for (int i = 0; i <= maxElem && i < _priorityTickets.Count; i++)
                {
                    if (i >= min && i <= maxElem)
                    {
                        actual.Add(_priorityTickets[i]);
                    }
                }
                dataListPriorityTickets.DataSource = actual.Reverse();
                dataListPriorityTickets.DataBind();
                MultiViewPriority.ActiveViewIndex = 0;
            }
        }

        protected void DL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            long ticketid = long.Parse(e.CommandArgument.ToString(), CultureInfo.InvariantCulture);
            Response.Redirect("ViewTicket.aspx?ticketId=" + ticketid);
        }

        protected void PrevButton_Click(object sender, EventArgs e)
        {
            int pageNum = int.Parse(Session["newestNumPage"].ToString());
            pageNum--;
            Session["newestNumPage"] = pageNum;
            ChangeNewestTicketsPage();
        }

        protected void NextButton_Click(object sender, EventArgs e)
        {
            int pageNum = int.Parse(Session["newestNumPage"].ToString());
            pageNum++;
            Session["newestNumPage"] = pageNum;
            ChangeNewestTicketsPage();
        }

        protected void PrevButton2_Click(object sender, EventArgs e)
        {
            int pageNum = int.Parse(Session["priorityNumPage"].ToString());
            pageNum--;
            Session["priorityNumPage"] = pageNum;
            ChangePriorityTicketsPage();
        }

        protected void NextButton2_Click(object sender, EventArgs e)
        {
            int pageNum = int.Parse(Session["priorityNumPage"].ToString());
            pageNum++;
            Session["priorityNumPage"] = pageNum;
            ChangePriorityTicketsPage();
        }
    }
}